package com.byxy.ssm.demo.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.ssm.demo.dao.ForumDao;
import com.byxy.ssm.demo.dao.TabDao;
import com.byxy.ssm.demo.dao.TipDao;
import com.byxy.ssm.demo.dao.UserDao;
import com.byxy.ssm.demo.entity.Forum;
import com.byxy.ssm.demo.entity.Tab;
import com.byxy.ssm.demo.entity.Tip;
import com.byxy.ssm.demo.entity.User;
import com.byxy.ssm.demo.service.MainService;
import com.byxy.ssm.demo.vo.TipVO;

@Service
@Transactional
public class MainServiceImpl implements MainService {

	@Autowired
	private TipDao tipDao;
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private TabDao tabDao;
	@Autowired
	private ForumDao forumDao;
	@Override
	public List<TipVO> find() {
		List<Tip> tips = tipDao.find();
		List<TipVO> tipvos = new ArrayList<>();
		
		///// 组合数据
		for(Tip p : tips) {
			TipVO vo = new TipVO();
			BeanUtils.copyProperties(p, vo);
			User u = userDao.getById(p.getUserId());
			if(u.getUserNick()!=null && u.getUserNick().equals(""))
				vo.setUserName(u.getUserNick());
			else
				vo.setUserName(u.getUserName());
					
			Tab tab = tabDao.getById(p.getTabId());
			
			vo.setTabName(tab.getTabName());
			Forum forum = forumDao.getById(tab.getForumId());
			vo.setForumName(forum.getForumName());
			tipvos.add(vo);
		}
		
		return tipvos;
	}

}
